module MC.View {
    export class PhotoClip {
        public constructor() {
            MC.loadJsUrl('/h5/my/touxiang/js/PhotoClip.js', () => {
                this.init();
            });
        }

        private fun: Function = null;
        private TxDiv: JQuery = null;

        private init() {
            let mthis = this;
            this.TxDiv = $(`
			<div class="TXupfileDiv">
				<div class="posfileDiv">
					<div class="upfile">
						<input type="file" class="filebtn">
					</div>
				</div>
        		<div id="clipArea"></div>
				<div class="bottomDiv"  >
					<div class="btnList clearfix">
						<a class="button" id="clipBtn">保存</a>
						<a class="button " id="closebtn">关闭</a>
					</div>
				</div>
					<div class="viewDiv">
						<div id="view"></div>
					</div>
    			</div>
			`);
            $('body').append(this.TxDiv);
            this.TxDiv.hide();
            var base64 = '';
            var pc = new window.PhotoClip('#clipArea', {
                size: [260, 260],
                outputSize: 640,
                //adaptive: ['60%', '80%'],
                file: '.TXupfileDiv .filebtn',
                view: '#view',
                ok: '#clipBtn',
                //img: 'img/mm.jpg',
                loadStart: function () {
                    console.log('开始读取照片');
                },
                loadComplete: function () {
                    console.log('照片读取完成');
                    $('.TXupfileDiv .posfileDiv').hide();
                    $('.TXupfileDiv #clipArea,.TXupfileDiv .bottomDiv #clipBtn').css('display', 'block');
                },
                done: function (dataURL: any) {
                    //  console.log(dataURL);
                    base64 = dataURL;
                    //  $('#saveBtn,.viewDiv').css('display', 'block');
                    if (base64 == '') {
                        MC.msg.alert('出现错误,请重试');
                        return;
                    }
                    MC.apiD("getIdentification", {}, (identification_password: any) => {
                        MC.apiD("http://test.fuzhuangchangongxiao.cn/api/app/call/upUserPicture", {
                            base64: base64,
                            identification_password:identification_password
                        }, function (json: any) {
                            mthis.TxDiv.hide();
                            mthis.fun(json);
                        });
                    });


                },
                fail: function (msg: any) {
                    alert(msg);
                }
            });
            this.TxDiv.find('#closebtn').on('click', () => {
                mthis.TxDiv.hide();
            });
        }


        public selPic(fun: Function) {
            this.fun = fun;
            this.TxDiv.show();
            this.TxDiv.find('.posfileDiv').show();
            this.TxDiv.find('#clipArea').hide();
            this.TxDiv.find('#clipBtn').hide();
        }
    }
}